package com.chen.androidstudy.activity;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.chen.androidstudy.R;

/**
 * @Author: CHong
 * @CreateDate: 2024/7/9 0009
 * @Description:
 */

public class SendSmsActivity extends AppCompatActivity implements View.OnClickListener {
    private PendingIntent mPiResult;
    private sendReceiver mReceiver;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms);
        TextView txtSend = findViewById(R.id.txtSend);
        txtSend.setOnClickListener(this);

        String action = "com.pocketdigi";

        mReceiver = new sendReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);
        registerReceiver(mReceiver, filter);
        Intent intent = new Intent(action);
        mPiResult = PendingIntent.getBroadcast(this, 0, intent, 0);
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.txtSend) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("17302818026", null, "message", mPiResult, null);
            Toast.makeText(this, "发送了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被授予，发送短信
                Toast.makeText(SendSmsActivity.this, "有发短信的权限", Toast.LENGTH_SHORT).show();
            } else {
                // 权限被拒绝，可以提示用户或者采取其他行动
                Toast.makeText(SendSmsActivity.this, "无权限", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mReceiver != null){
            unregisterReceiver(mReceiver);
            mReceiver = null;
        }
    }

    class sendReceiver extends BroadcastReceiver {
        //写个接收器
        @Override
        public void onReceive(Context context, Intent intent) {
            int resultCode = getResultCode();
            if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(SendSmsActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(SendSmsActivity.this, "发送失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
